<template>
	<view :style="'height:' +windowHeight+'px'">

		<view class="head">
			<view class="" :style="{'height':  statusBarHeight+'px'}"></view>
			<view class="head__box" :style="{'width':windowWidth+'px','height':  naviBarHeight+'px'}">
				<view @click="isBack" class="head__box__img">
					<image src="/static/icon/fanhui.png" style="width: 35rpx;height: 35rpx;" mode=""></image>
				</view>
				<view style="font-size: 32rpx;color: #000000;">我的书单</view>
			</view>
		</view>
		<view class="" :style="{'height': Number(statusBarHeight + naviBarHeight)+'px'}"></view>



		<view class="search">
			<view class="search__box">
				<input class="search__box__input" confirm-type="text" type="text" @confirm="searchText" v-model="title" />
				<view class="search__box__content" v-if="title == ''">
					<image class="search__box__content__img" src="/static/icon/search.png" mode=""></image>
					<view class="search__box__content__text">
						<text>请输入关键词</text>
					</view>
				</view>
				<view class="search__box__icon" v-if="title != ''" @click="reset">
					<image class="search__box__icon__img" src="/static/icon/password_icon_del.png" mode=""></image>
				</view>
			</view>
		</view>



		<scroll-view scroll-x="false" @scroll="scroll" :refresher-enabled="enabled" @refresherrefresh="refresherrefresh"
		 @refresherrestore="refresherrestore" :refresher-triggered="triggered"  v-if="is_occupied"
		 scroll-y="true" @scrolltolower="initMore" :style="'height:'+ Number(windowHeight - 60 -  Number(statusBarHeight + naviBarHeight) - 49)+'px'">
			<view class="scroll_box">
				<view class="scroll_box__unit" v-for="(item,index) in my_book_list" :key='index' :class="item.isReceive?'select_book':''"
				 @click="selectBook(index,item.bookStatus)">
					<view class="" style="display: flex;flex-direction: column;align-items: center;position: relative;">
						<image :src="item.bookImg" style="width:140rpx;height: 220rpx;z-index: 6;" mode="aspectFill"></image>

						<image src="../../static/img/bookZuo.png" style="width:250rpx;height: 100rpx;position: absolute;bottom: -70rpx;"
						 mode=""></image>
					</view>
					<view class="content">
						<view class="" style="display: flex;flex-direction: column;">
							<view class="" style="font-size: 38rpx;font-weight: 700;color: rgb(57,57,56);">
								{{item.bookName}}
							</view>
							<view class="" style="color: rgb(108,108,107);font-size: 24rpx;">
								{{item.bookAuthor}}
							</view>
						</view>
						<view class="" style="display: flex;flex-direction: column;">
							<view class="" style="color: rgb(108,108,107);font-size: 24rpx;">
								类型：<text>{{item.typeId_dictText}}</text>
							</view>
							<view class="" style="color: rgb(108,108,107);font-size: 24rpx;">
								位置：<text>{{item.bookPosition}}</text>
							</view>

							<view class="" style="color: rgb(108,108,107);font-size: 24rpx;">
								状态：<text v-if="item.bookStatus == 1" style="color: rgb(0,217,54);">{{item.bookStatus_dictText}}</text>
								<text v-else style="color: rgb(218,168,31);">{{item.bookStatus_dictText}}</text>
							</view>
						</view>
					</view>
				</view>


			</view>
			<view class="" v-if="isBorrow" style="display: flex;align-items: center;justify-content: center;padding: 50rpx 0;width: 100%;">
				<view class=""  @click="booksApply()" style="padding:15rpx 40rpx ;background-color: rgb(0,116,208);border-radius: 12rpx;color: #FFFFFF;">
					借阅
				</view>
				<view class="" @click="removeBook()" style="padding:15rpx 40rpx ;background-color: #e96c6e;border-radius: 12rpx;color: #FFFFFF;margin-left: 133rpx;">
					删除
				</view>
			</view>
			<view class="" v-if="!isBorrow && isRemove" style="display: flex;align-items: center;justify-content: center;padding: 50rpx 0;width: 100%;">
				<view class="" @click="removeBook()" style="padding:15rpx 40rpx ;background-color: #e96c6e;border-radius: 12rpx;color: #FFFFFF;">
					删除
				</view>
			</view>

		</scroll-view>

		<view class="" v-if="!is_page && no_kong" 
		 style="width: 100%;display: flex;align-items: center;justify-content: center;position: fixed;top: 40%;">
			<image src="/static/icon/kong_icon_bg.png" style="width: 422rpx;height: 298rpx;" mode=""></image>
		</view>
	

	</view>
</template>

<script>
	import {
		Debounce
	} from '@/common/utils.js' // 防抖函数（减少函数的调用）

	export default {
		data() {
			return {
				my_book_list: [], //我的借阅列表
				pageNo: 1,
				pageSize: 10,
				is_page: true,
				no_kong: false,
				title: '',
				is_occupied: false,

				enabled: true,
				triggered: false,

			};
		},
		props: {
			windowWidth: { //系统界面宽度
				type: Number,
				default () {
					return 0;
				}
			},
			windowHeight: { //系统界面高度
				type: Number,
				default () {
					return 0;
				}
			},
			statusBarHeight: { //状态栏高度
				type: Number,
				default () {
					return 0;
				}
			},
			naviBarHeight: { //导航栏高度
				type: Number,
				default () {
					return 0;
				}
			}
		},
		mounted() {
			let self = this
			this.init()
			uni.$on('my_book',function() {
				this.pageNo = 1
				this.title = ''
				self.my_book_list = []
				self.init()
			})
		},
		methods: {

			init() {
				this.$bookLending.fAppBooksBookListList({
					pageNo: this.pageNo,
					pageSize: this.pageSize,
					bookName: this.title
				}).then((res) => {
					var data = res.data.result.records
					console.log(data)
					if (data.length > 0) {
						this.is_page = true
						data.forEach(val => val.isReceive = false)
						this.my_book_list = this.my_book_list.concat(data);
						if (this.my_book_list.length > 0) {
							this.is_occupied = true
						} else {
							this.is_occupied = false
						}
					} else {
						if(!this.my_book_list.length) {
							this.no_kong = true
						}
						this.is_page = false
					}
					this.$forceUpdate()
				}).catch(error => {
					console.log(error)
				})
			},
	
			isBack() {
				this.$emit('isBack');
			},
			//选择书籍
			selectBook(index, isBorrow) {

				this.my_book_list[index].isReceive = !this.my_book_list[index].isReceive
				this.$forceUpdate()

			},
			//借阅书籍
			booksApply() {

				var userInfo = uni.getStorageSync('userInfo')

				var param = {
					id: userInfo.id,
					bookids: [],
				}
				this.my_book_list.filter(val => val.isReceive && val.bookStatus == 1).forEach((item, index) => {
					param.bookids.push(item.id)
				})

				this.$bookLending.fAppBooksApply(param).then((res) => {
					uni.showToast({
						title: '借阅成功',
						duration: 2000,
						icon: 'none'
					});
					
					
					this.my_book_list = this.my_book_list.filter(val => !val.isReceive || val.isReceive && val.bookStatus != 1)
					uni.$emit('updateBorrowList_event', {
						msg: '页面更新'
					})

					this.$forceUpdate()
				}).catch(err => {
					uni.showToast({
						title: err.data.message,
						duration: 2000,
						icon: 'none'
					});
				})
			},

			initMore() {
				if (this.is_page) {
					this.pageNo++
					this.init()
				}
			},
			searchText: Debounce(
				function() {
					this.pageNo = 1
					this.my_book_list = []
					this.init()
				}),
			reset() {
				this.title = ''
				this.pageNo = 1
				this.my_book_list = []
				this.init()

			},
			scroll(e) {
				if (e.target.scrollTop <= 45) {
					this.enabled = true
				} else {
					this.enabled = false
				}


			},

			refresherrefresh(e) {
				this.pageNo = 1
				this.triggered = true;

				this.$bookLending.fAppBooksBookListList({
					pageNo: this.pageNo,
					pageSize: this.pageSize,
					bookName: this.title
				}).then((res) => {
					this.triggered = false
					this.my_book_list = []
					var data = res.data.result.records
					if (data.length > 0) {
						this.is_page = true
					} else {
						this.is_page = false
					}
					data.forEach(val => val.isReceive = false)
					this.my_book_list = this.my_book_list.concat(data);
					if (this.my_book_list.length > 0) {
						this.is_occupied = true
					} else {
						this.is_occupied = false
					}
					this.$forceUpdate()
				})


			},
			refresherrestore() {
				// uni.showToast({
				// 	title: '刷新成功',
				// 	duration: 2000,
				// 	icon: 'none'
				// });
			},
			//删除图书
			removeBook() {
				var idArr = this.my_book_list.filter(val => val.isReceive)

				for (var i = 0; i < idArr.length; i++) {
					let bookId =  idArr[i].id
					this.$bookLending.fAppBooksdelBookList({
						id: bookId
					}).then((res) => {
						console.log(res)
						this.my_book_list = this.my_book_list.filter(item => item.id != bookId)
						if(idArr.length-1 == i){
							uni.showToast({
								title: '删除成功',
								duration: 2000,
								icon: 'none'
							});
						}
						if (this.my_book_list.length > 0) {
							this.is_occupied = true
						} else {
							this.is_occupied = false
						}
						this.$forceUpdate()
					}).catch(error => {
					
						uni.showToast({
							title: '删除失败',
							duration: 2000,
							icon: 'none'
						});
					
					// 	this.my_book_list = this.my_book_list.filter(item => item.id != bookId)
		
						this.$forceUpdate()
					})
				}


			}


		},
		computed: {
			isRemove() {
				if (this.my_book_list.filter(val => val.isReceive).length == 0) {
					return false
				} else {
					return true
				}
			},
			isBorrow() {
				if (this.my_book_list.filter(val => val.isReceive).length == 0) {
					return false
				} else {
					if(this.my_book_list.filter(val => val.isReceive && val.bookStatus == 1).length > 0){
						return true
					}else{
						return false
					}
					
				}
			}
		}
	}
</script>

<style lang="scss">
	.head {
		position: fixed;top: 0;left: 0;right: 0;background-color: #FFFFFF;
		&__box {
			display: flex;align-items: center;justify-content: center;position: relative;
			&__img {
				position:absolute;left: 43rpx;height: 100%;display: flex;align-items: center;justify-content: center;font-size: 32rpx;color: #000000;
			}
			&__remove {
				position:absolute;right: 43rpx;height: 100%;display: flex;align-items: center;justify-content: center;font-size: 32rpx;color: #D96058;
			}
		}
	}
	.search {
		width: 100%;
		height: 94rpx;
		background-color: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: center;

		&__box {
			width: 700rpx;
			height: 80rpx;
			background-color: #F6F6F6;
			border-radius: 30rpx;
			display: flex;
			align-items: center;

			&__input {
				width: 600rpx;
				height: 80rpx;
				position: relative;
				left: 30rpx;
			}

			&__content {
				height: 80rpx;
				background-color: #F6F6F6;
				display: flex;
				align-items: center;
				justify-content: center;
				position: absolute;
				left: 74rpx;

				&__img {
					width: 24rpx;
					height: 24rpx;
				}

				&__text {
					height: 80rpx;
					border-radius: 30rpx;
					display: flex;
					align-items: center;
					font-size: 30rpx;
					color: rgba(153, 153, 153, 1);
					margin-left: 13rpx;
				}
			}

			&__icon {
				width: 30rpx;
				height: 80rpx;
				border-radius: 50%;
				margin-left: 42rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				&__img {
					width: 30rpx;
					height: 30rpx;
					border-radius: 50%;
				}
			}
		}
	}

	.text-oneline {
		display: inline-block;
		white-space: nowrap;
		width: 100%;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.scroll_view_item {
		border-bottom: 1px solid rgb(240, 240, 238);
		padding: 20rpx 20rpx 20rpx 0rpx;
		display: flex;
		align-items: center;
		margin-left: 40rpx;
	}


	.scroll_box {
		width: 100%;
		padding: 35rpx 35rpx 0rpx 35rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		flex-wrap: wrap;
		box-sizing: border-box;

		&__unit {
			width: 100%;
			height: 330rpx;
			border-radius: 12rpx;
			display: flex;
			align-items: center;
			padding-left: 60rpx;
			margin-bottom: 30rpx;

			.content {
				height: 250rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				margin-left: 68rpx;
			}
		}
	}

	.select_book {
		border: 1px solid rgb(97, 166, 220);
	}
</style>
